import { Controller, Get, Post, Put, Delete, Body, Param, Query } from '@nestjs/common';
import { UserService } from './user.service';

@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) { }

  @Post()
  create(@Body() data: any) {
    return this.userService.create(data);
  }

  @Put(':id')
  update(@Param('id') id: string, @Body() data: any) {
    return this.userService.update(+id, data);
  }

  @Delete(':id')
  delete(@Param('id') id: string) {
    return this.userService.delete(+id);
  }

  @Get()
  findAll(@Query() query: any) {
    const { page = 1, size = 10, ...where } = query;
    return this.userService.findAll({
      skip: (page - 1) * size,
      take: +size,
      where
    });
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.userService.findOne(+id);
  }

  @Put(':id/password')
  resetPassword(@Param('id') id: string, @Body('password') password: string) {
    return this.userService.resetPassword(+id, password);
  }

  @Put(':id/roles')
  updateUserRoles(@Param('id') id: string, @Body('roleIds') roleIds: number[]) {
    return this.userService.updateUserRoles(+id, roleIds);
  }
}
